﻿#include <QSqlError>
#include "ViewWholeStandard.h"
#include "DatabaseMgmt.h"

CViewWholeStandard::CViewWholeStandard()
{

}

CViewWholeStandard::~CViewWholeStandard()
{

}

int CViewWholeStandard::queryAll(QVector<ViewWholeStandard>& standards)
{
	int ret = 0;

	QString strSql = QString("SELECT KIND_ID, KIND_NAME, STANDARD_ID, STANDARD_NAME, STANDARD_MIN_VALUE,"
		" STANDARD_MXN_VALUE, STANDARD_MAINLY FROM VIEW_GOODS_INFO");

	QSqlQuery query;
	CDatabaseMgmt::getInstance()->execSql(strSql, query);

	if (query.lastError().type() != QSqlError::NoError)
	{
		ret = CONST_ERR_EXEC_SQL_FAILED;
	}
	else
	{
		while (query.next())
		{
			ViewWholeStandard stand;

			stand.kind_id = query.value("KIND_ID").toUInt();
			stand.kind_name = query.value("KIND_NAME").toString();
			stand.standard_id = query.value("STANDARD_ID").toUInt();
			stand.standard_name = query.value("STANDARD_NAME").toString();
			stand.standard_min_value = query.value("STANDARD_MIN_VALUE").toDouble();
			stand.standard_max_value = query.value("STANDARD_MXN_VALUE").toDouble();
			stand.standard_mainly = query.value("STANDARD_MAINLY").toUInt();

			standards.append(stand);
		}
	}
	return ret;
}

int CViewWholeStandard::removeById(uint32_t uid)
{
	int ret = CONST_ERR_SUCCESS;

	QString strSql = QString("DELETE FROM TB_GOODS_STANDARD WHERE UID=%1")
		.arg(uid);

	QSqlQuery query;
	CDatabaseMgmt::getInstance()->execSql(strSql, query);

	if (query.lastError().type() != QSqlError::NoError)
	{
		ret = CONST_ERR_EXEC_SQL_FAILED;
		qInfo() << query.lastError().text();
	}

	return ret;
}

int CViewWholeStandard::removeByStandardId(uint32_t uid)
{
	int ret = CONST_ERR_SUCCESS;

	QString strSql = QString("DELETE FROM TB_GOODS_STANDARD WHERE KIND_ID=%1")
		.arg(uid);

	QSqlQuery query;
	CDatabaseMgmt::getInstance()->execSql(strSql, query);

	if (query.lastError().type() != QSqlError::NoError)
	{
		ret = CONST_ERR_EXEC_SQL_FAILED;
		qInfo() << query.lastError().text();
	}

	return ret;
}
